package com.example.adapter;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.activity.BillDetail_Activity;
import com.example.bean.GridViewBean;
import com.example.db.DBService;
import com.example.zhaccount.MainActivity;
import com.example.zhaccount.R;

public class ListViewAdapterBill extends BaseAdapter implements OnItemClickListener{
	private Context mContext;
	private List<GridViewBean> list;
	
	private DBService dbService;
	private GridViewBean findGVB;
	private View popRootView;
	private PopupWindow mPopwindow;
	private Handler mHandler = new Handler();
	private Intent mIntent;
	
	public ListViewAdapterBill(Context mContext, List<GridViewBean> list) {
		super();
		this.mContext = mContext;
		this.list = list;
		dbService = new DBService(mContext);
		
		//初始化listView单击时的界面
		LayoutInflater lay = (LayoutInflater) mContext.
				getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		popRootView = lay.inflate(R.layout.lsitem_pop_lv, null);
		mPopwindow = new PopupWindow(popRootView,
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);
		popRootView.setFocusable(true);
		mPopwindow.setFocusable(true);
		mPopwindow.setOutsideTouchable(true);
		mPopwindow.setBackgroundDrawable(new BitmapDrawable());
	}

	@Override
	public int getCount() {
		if (list != null) {
			return list.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		HolderView holder = null;
		if (convertView == null) {
			holder = new HolderView();
			convertView = LayoutInflater.from(mContext)
					.inflate(R.layout.listview_item, null);
			holder.imgBill = (ImageView) convertView.findViewById(R.id.lvItem_img);
			holder.textBill = (TextView) convertView.findViewById(R.id.lvItem_outtext);
			holder.textBill_zou = (TextView) convertView.findViewById(R.id.lvItem_intext_zou);
			convertView.setTag(holder);
		}else{
			holder = (HolderView) convertView.getTag();
		}
		holder.imgBill.setImageResource(list.get(position).getImg());
		int state = Integer.parseInt(list.get(position).getState());
		String type = list.get(position).getType();
		String money = list.get(position).getMoney();
		if (state == 1) {
			holder.textBill.setVisibility(View.VISIBLE);
			holder.textBill_zou.setVisibility(View.INVISIBLE);
			holder.textBill.setText(type + "  "+String.format("%.2f", Double.parseDouble(money)));
		}
		if (state == 2) {
			holder.textBill.setVisibility(View.GONE);
			holder.textBill_zou.setVisibility(View.VISIBLE);
			holder.textBill_zou.setText(String.format("%.2f", Double.parseDouble(money))+"  "+type);
		}
		return convertView;
	}

	
	class HolderView {
		ImageView imgBill;
		TextView textBill,textBill_zou;
	}


	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		findGVB = list.get(position);
		int[] location = new int[2];
		view.getLocationOnScreen(location);
		mPopwindow.showAtLocation(view, Gravity.NO_GRAVITY, 0, location[1]);
		ImageView imgEdit = (ImageView) popRootView.findViewById(R.id.lvItem_edit);
		ImageView imgDel = (ImageView) popRootView.findViewById(R.id.lvItem_del);
		imgEdit.setOnClickListener(new MyOnClick());
		imgDel.setOnClickListener(new MyOnClick());
	}
	
	//编辑  删除  OnClickListener点击事件
	class  MyOnClick implements OnClickListener{

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.lvItem_edit:
				mIntent = new Intent(mContext, BillDetail_Activity.class);
				mIntent.putExtra("chooseGVB", findGVB);
				mContext.startActivity(mIntent);
				break;
			case R.id.lvItem_del:
				deleteById();
				break;
			default:
				break;
			}
		}
	}
	
	
	public void deleteById(){
		new AlertDialog.Builder(mContext)
		.setMessage("确定要删除?")
		.setPositiveButton("确定", 
				new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
						String delId = String.valueOf(findGVB.getId());
						long e = dbService.deleBill(delId);
						if (e != -1) {
							notifyDataSetChanged();
							mHandler.postDelayed(new Runnable() {
								
								@Override
								public void run() {
									mIntent = new Intent(mContext, MainActivity.class);
									mContext.startActivity(mIntent);
								}
							}, 300);
							
						}else{
							Toast.makeText(mContext, "delete fail!", Toast.LENGTH_LONG).show();
						}
					}
				})
		.setNegativeButton("取消", 
				new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
					}
				}).create().show();
	}

}
